# # 打开文件,默认读模式,使用变量f接收
# 文件中有中文内容时,需要设置编码为:utf-8
f = open('testDemo.txt', encoding='utf-8')
# f.read()读取出文件中的内容,赋值给value
# value = f.read()
# f.close()
# # 打印value中的信息
# print(value)

# # with as运行完成后会自动关闭打开的文件,释放内存,建议使用
# with open("testDemo.txt") as file:
#     print(file.read())
# print(1215152)

# 读文件
# with open(file='testDemo.txt', encoding="utf-8") as f:
    # read()读取文件中所有内容
    # count1 = f.read()
    # print(count1)
    # count2 = f.read()
    # print(count2)
    # readline()读取光标所在行,可以传数字,指定读取多少字
    # count3 = f.readline()
    # print(count3, end="")
    # count4 = f.readline(2)
    # print(count4)
    # print(f.readline(2))
    # # tell()获取指针当前位置,汉字占3个位置
    # value = f.tell()
    # print(value)
    # f.readline()
    # print(f.tell())
    # # 设置指针的位置
    # f.readline(2)
    # print(f.tell())
    # f.seek(1)
    # print(f.tell())

# 写文件
# # r+ 从指针为0的位置开始写,覆盖内容
# with open('testDemo.txt', 'r+', encoding='utf-8') as f:
#     f.write("坚果")
# # w 覆盖文件内所有内容内容
# with open('testDemo.txt', 'w', encoding='utf-8') as f:
#     f.write("坚果")
# # a+,表示追加
# with open('testDemo.txt', 'a+', encoding='utf-8') as f:
#     f.write("坚果")